package com.recursion;

import java.util.ArrayList;
import java.util.List;

public class MenuTree {
    private List<Menu> menuList = new ArrayList<Menu>();
    public MenuTree(List<Menu> menuList){
        this.menuList = menuList;
    }
    //建立树形结构
    public List<Menu> buildTree(){
        List<Menu> treeMenus = new ArrayList<Menu>();
        for(Menu menuNode : getRootNode()){
            menuNode = buildChildTree(menuNode);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树型结构
    private Menu buildChildTree(Menu pNode){
        List<Menu> childMenu = new ArrayList<Menu>();
        for(Menu menuNode : menuList){
            if(menuNode.getParentId().equals(pNode.getId())){
                childMenu.add(buildChildTree(menuNode));
            }
        }
        pNode.setChildren(childMenu);
        return pNode;
    }

    //获取根节点
    private List<Menu> getRootNode(){
        List<Menu> rootMenuLists = new ArrayList<Menu>();
        for(Menu menuNode : menuList){
            if(menuNode.getParentId().equals("0")){
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
